<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/28/028
 * Time: 23:25
 */

namespace app\api\controller;

use think\Db;
use think\Request;

class News extends Rest
{

    public function index()
    {

    }

    public function getNews()
    {
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $data = Db::name('news')->where(['data_state' => 1])->order('datatime desc')->page($this->page_num, 4)->select();
        foreach ($data as $key => $value) {
            if ($value['thumimg']) {
                $imgs = explode(',', $value['thumimg']);
                $data[$key]['thumimg'] = $imgs;
            } else {
                $data[$key]['thumimg'] = [];
            }
            if ($value['datatime']) {
                $strtimes = explode(" ", $value['datatime']);
                $timearray = explode("-", $strtimes[0]);
                $year = $timearray[0];
                $month = $timearray[1];
                $day = $timearray[2];
                $data[$key]['year'] = $year;
                $data[$key]['month'] = $month;
                $data[$key]['day'] = $day;
            }
        }
        $this->data['msg'] = '获取成功';
        $this->data['data'] = $data;
        return $this->data;
    }
    public function getNewsLists()
    {
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $request = Request::instance();
        $data = $request->param();
        $page = $data['page'];
        $count = Db::name('news')->where(['data_state' => 1])->order('datatime desc')->count();
        $data = Db::name('news')->where(['data_state' => 1])->order('datatime desc')->page($page, $this->page_limit)->select();
        foreach ($data as $key => $value) {
            if ($value['thumimg']) {
                $imgs = explode(',', $value['thumimg']);
                $data[$key]['thumimg'] = $imgs;
            } else {
                $data[$key]['thumimg'] = [];
            }
            if ($value['datatime']) {
                $strtimes = explode(" ", $value['datatime']);
                $timearray = explode("-", $strtimes[0]);
                $year = $timearray[0];
                $month = $timearray[1];
                $day = $timearray[2];
                $data[$key]['year'] = $year;
                $data[$key]['month'] = $month;
                $data[$key]['day'] = $day;
            }
        }
        $this->data['counts'] = $count;
        $this->data['msg'] = '获取成功';
        $this->data['data'] = $data;
        return $this->data;
    }

    public function getNewsDetails()
    {
//        if (1 != self::check_power(1)) {
//            return $this->data;
//        }
        $request = Request::instance();
        $data = $request->param();
        $newsid = $data['newsid'];
        Db::name("news")->where(["id" => $newsid])->setInc("hits",1);
        $newsdetails = Db::name("news")->where(["id" => $newsid])->find();
        $this->data['msg'] = '获取成功';
        $this->data['data'] = $newsdetails;
        return  $this->data;
    }



    public function addnewslikes(){
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $request = Request::instance();
        $data = $request->param();
        $newsid = $data['newsid'];
        Db::name("news")->where(["id" => $newsid])->setInc("likes",1);
        $likes = Db::name("news")->where(["id" => $newsid])->value("likes");
        $this->data['msg'] = '获取成功';
        $this->data['data'] = $likes;
        return $this->data;
    }

    public function editNews()
    {

    }

    public function addNews()
    {

    }

    public function delNews()
    {

    }

}